home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Core / ObjectNS.cpp < prev    next >
Encoding:
Text File  |  1997-02-13  |  11.8 KB  |  388 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ObjectNS.cpp
  3.  
  4.     Contains:    Implementation of ODObjectNameSpace class
  5.  
  6.     Owned by:    Ed Lai
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>     9/23/96    EL        1242543: Throw in register for wrong type.
  13.         <13>     10/8/95    TJ        Fixes Recomended by Refball
  14.         <12>     8/29/95    DM        1279507: make GetEntry() consistently
  15.                                     return null object when it returns
  16.                                     false.1279569: throw if null after newing
  17.                                     SOM instances
  18.         <11>      8/3/95    RR        #1257260: Collapse B classes. Remove
  19.                                     somInit methods. Don't call IsInitialized
  20.                                     or SubclassResponsibility
  21.         <10>     7/24/95    CG        #1270320: Memory leak fixes.
  22.          <9>     7/14/95    TJ        Jens added a test to check for non object
  23.                                     deletions.
  24.          <8>     5/11/95    DM        #1181622: Updated file ownership.
  25.          <7>      5/1/95    CG        1241627    BB: SOM_TRY blocks needed in Core
  26.                                     SOM methods
  27.          <6>     4/26/95    CG        1211082 BB: 5$ Bugs need to be evaluated
  28.                                     and removed from Core
  29.          <5>    12/13/94    CG        #1203295:  somUninit disposes of all
  30.                                     objects.
  31.          <4>     8/31/94    TÇ        #1183129, #1183116, #1183119, #1183111:
  32.                                     Lots of ErrorCode cleanup.
  33.          <3>     8/24/94    CG        #1181622: Updated file ownership.
  34.          <2>     8/18/94    jpa        Deleted obsolete I/O methods [1181839]
  35.          <1>     7/29/94    CG        first checked in
  36.          <2>      7/1/94    CG        Moved ReadSize and ReadBytes to
  37.                                     BndNSUtl.cpp.
  38.          <1>     6/24/94    CG        first checked in
  39.          <0>     6/24/94    SV        SOMverted
  40.          <8>     5/27/94    jpa        New exception support [1165267]
  41.          <7>      5/9/94    MB        #1162181: Changes necessary to install MMM.
  42.          <6>      4/1/94    TÇ        #1154941: Get OpenDoc source to actually
  43.                                     compile for PowerPC
  44.          <5>     3/28/94    CG        1152929: Created new error
  45.                                     kInvalidFileContents to throw when
  46.                                     Read fails.
  47.          <4>     3/25/94    TÇ        removed erroneous comment: // $$$$ POWRPC
  48.                                     NATVE
  49.          <3>     3/15/94    MB        Changes to support SCpp/ASLM builds,
  50.                                     #1150864.
  51.          <2>     2/15/94    CG        #1143680 - NameSpace now being allocated on
  52.                                     specified heap.
  53.          <6>      2/8/94    TÇ        Throw -> THROW & some code clean up
  54.          <5>      2/4/94    TÇ        SysEqu -> LowMem
  55.          <4>      2/3/94    CG        Added _fHeap field to store current heap.
  56.          <3>     1/29/94    NP        Init changes.
  57.          <2>     1/18/94    CG        Changed GetValue to take valueLen parameter.
  58.          <1>     1/14/94    CG        first checked in
  59.         <40>     1/12/94    CG        Fixed bug in LoadNameSpaceTable().
  60.         <39>     1/11/94    TÇ        Init... changes
  61.         <38>    12/17/93    eeh        implement GetRegLibFileFolder
  62.         <37>    12/15/93    TÇ        more InitObject changes, remove
  63.                                     Initialize() method
  64.         <36>    12/15/93    TÇ        InitObject Changes
  65.         <35>    12/10/93    CG        Use HasNameSpace where appropriate.
  66.         <34>     12/6/93    CG        fixed NMAP reading bug.
  67.         <33>     12/3/93    TÇ        Stop including ODError.h, it is included
  68.                                     as ErrorDef.h inside Except.h
  69.         <32>    11/18/93    CG        Changed prefs file format, mapping rez
  70.                                     format.  Fixed bugs.
  71.         <31>    11/15/93    NP        Changed XMPSymbols to ODNameSpaceManager.
  72.                                     Fixed bugs.
  73.         <30>    11/12/93    CG        The Symbols object  now uses ReadFromFile
  74.                                     and WriteToFile for reading and writing to
  75.                                     prefs file.
  76.         <29>     11/8/93    NP        Added WriteToStorage and ReadFromStorage.
  77.         <28>     11/8/93    NP        Tweak ReadFromFile and WriteToFile to work
  78.                                     with refnum.
  79.         <27>     11/5/93    NP        Adding streaming functionality.
  80.         <26>     11/2/93    NP        Cast for CFront.
  81.         <25>     11/2/93    NP        Bug fixes.
  82.         <24>     11/2/93    CG        Fix for hash table changes.
  83.         <23>    10/28/93    NP        Comment out obsoleted parts of API. Will
  84.                                     remove later.
  85.         <22>    10/22/93    NP        Mods for new XMPAEHashTable and
  86.                                     StringHashTable protocols.
  87.         <21>    10/18/93    CG        Inability to creat prefs file no longer
  88.                                     prevents launch.
  89.         <20>    10/14/93    JA        Changed prefs file name to a fixed "OpenDoc
  90.                                     Prefs". (Using CurApName will not work
  91.                                     later on when every doc is a process!)
  92.         <18>     10/7/93    CG        Symbols now get written to prefs file.
  93.         <17>     10/5/93    CG        Added Exists method to ODNameSpace.
  94.         <16>     9/22/93    JA        Updated ASSERTs for new macro.
  95.         <15>     9/21/93    CG        Added namespace table loading.
  96.         <14>     9/20/93    CG        Fixed ASSERT.
  97.         <13>     7/21/93    NP        Changed ODValueIterator::FirstNextAux.
  98.         <12>      7/2/93    NP        Abstract/Concrete breakout.
  99.         <11>     6/22/93    NP        Fixed some bugs. Synced with header.
  100.         <10>     6/15/93    NP        Mods to allow compilation with
  101.                                     LibraryManager.h and a few bug fixes.
  102.          <9>     6/14/93    NP        Implemented inheritance.
  103.          <8>      6/10/93    RCR        Change to new Exception macros
  104.          <7>      6/7/93    NP        Added missing cast in HasNameSpace. CFront
  105.                                     complained, but Scpp did not.
  106.          <6>      6/4/93    NP        Implemented everything except inheritance
  107.                                     of name spaces.
  108.          <5>      6/2/93    NP        Basic functionality working. Missing:
  109.                                     Finding name space by name and iterator.
  110.          <4>     5/18/93    NP        Updated HasNameSpace.
  111.          <3>     4/28/93    NP        File name changes.
  112.          <2>     4/27/93    NP        Added Purge.
  113.          <1>     4/23/93    NP        first checked in
  114.  
  115.     To Do:
  116.         Get prefs file name from a string resource.
  117.     In Progress:
  118.     
  119.         Work around presence of non-objects in name space in somUninit.
  120. */
  121.  
  122. #ifndef _PLFMDEF_
  123. #include "PlfmDef.h"
  124. #endif
  125.  
  126.  
  127. #define ODObjectNameSpace_Class_Source
  128. #include <ObjectNS.xih>
  129.  
  130. #ifndef _STRHSHTB_
  131. #include "StrHshTb.h"
  132. #endif
  133.  
  134. #ifndef _EXCEPT_
  135. #include "Except.h"
  136. #endif
  137.  
  138. #ifndef _PASCLSTR_
  139. #include "PasclStr.h"
  140. #endif
  141.  
  142. #ifndef __STRING__
  143. #include <string.h>
  144. #endif
  145.  
  146. #ifndef __LIMITS__
  147. #include <limits.h>
  148. #endif
  149.  
  150. #ifndef __LOWMEM__
  151. #include <LowMem.h>
  152. #endif
  153.  
  154. #ifndef _ODTYPES_
  155. #include <ODTypes.h>
  156. #endif
  157.  
  158. #ifndef SOM_ODStorageUnitView_xh
  159. #include <SUView.xh>
  160. #endif
  161.  
  162. #ifndef _ODMEMORY_
  163. #include "ODMemory.h"
  164. #endif
  165.  
  166. #ifndef _ISOSTR_
  167. #include "ISOStr.h"
  168. #endif
  169.  
  170. #ifndef SOM_ODObjectIterator_xh
  171. #include <ObjctItr.xh>
  172. #endif
  173.  
  174. #ifndef _BNDNSUTL_
  175. #include "BndNSUtl.h"
  176. #endif
  177.  
  178. #ifndef _PLFMFILE_
  179. #include "PLFMFile.h"
  180. #endif
  181.  
  182. #ifndef _ODUTILS_
  183. #include <ODUtils.h>
  184. #endif
  185.  
  186. #ifndef _MEMMGR_
  187. #include <MemMgr.h>
  188. #endif
  189.  
  190. #pragma segment ODObjectNameSpace
  191.  
  192. //==============================================================================
  193. // Implementation notes
  194. //==============================================================================
  195.  
  196. /*
  197. Hash tables used for the implementation are lazily instantiated. They will be
  198. created at the first request to add an entry.
  199. */
  200.  
  201. //==============================================================================
  202. // Constants
  203. //==============================================================================
  204.  
  205. // for hash tables
  206.  
  207. const ODUShort        kOSTypeKeySize = sizeof(ODOSType);
  208. const ODUShort        kSLongKeySize = sizeof(ODSLong);
  209. const ODUShort        kODPtrSize = sizeof(ODPtr);
  210. const ODBoolean    kNotInSystemHeap = false;
  211.  
  212. //==============================================================================
  213. // Function Prototype
  214. //==============================================================================
  215.  
  216. //==============================================================================
  217. // Local Functions
  218. //==============================================================================
  219.  
  220. //==============================================================================
  221. // ODObjectNameSpace
  222. //==============================================================================
  223.  
  224. //------------------------------------------------------------------------------
  225. // ODObjectNameSpace: InitObjectNameSpace
  226. //------------------------------------------------------------------------------
  227.  
  228. SOM_Scope void  SOMLINK ODObjectNameSpaceInitObjectNameSpace(ODObjectNameSpace *somSelf, Environment *ev,
  229.                                         ODNameSpaceManager*    mgr,
  230.                                         ODNameSpace*        parent,
  231.                                         ODULong                numExpectedEntries,
  232.                                         ODISOStr            name)
  233. {
  234.     /* ODObjectNameSpaceData *somThis = ODObjectNameSpaceGetData(somSelf); */
  235.     ODObjectNameSpaceMethodDebug("ODObjectNameSpace","InitObjectNameSpace");
  236.     
  237.     SOM_TRY
  238.  
  239.     somSelf->SetType( ev, kODNSDataTypeODObject );
  240.     somSelf->InitNameSpace( ev, mgr, parent,numExpectedEntries, name);
  241.     
  242.     SOM_CATCH_ALL
  243.     SOM_ENDTRY
  244. }
  245.  
  246. //------------------------------------------------------------------------------
  247. // ODObjectNameSpace: ~ODObjectNameSpace
  248. //------------------------------------------------------------------------------
  249.  
  250. SOM_Scope void  SOMLINK ODObjectNameSpacesomUninit(ODObjectNameSpace *somSelf)
  251. {
  252.     /* ODObjectNameSpaceData *somThis = ODObjectNameSpaceGetData(somSelf); */
  253.     ODObjectNameSpaceMethodDebug("ODObjectNameSpace","somUninit");
  254.  
  255.     ODISOStr                key;
  256.     ODObject*                object;
  257.     ODULong                    length;
  258.     
  259.     TRY{
  260.  
  261.         Environment* ev = somGetGlobalEnvironment();
  262.         
  263.         ODObjectIterator*    iter = somSelf->CreateIterator(ev);
  264.         for (iter->First(ev, &key, &object, &length);
  265.                 iter->IsNotComplete(ev);
  266.                 iter->Next(ev, &key, &object, &length))
  267.         {
  268.             // This test is a workaround only; non-objects should not be stored here! $$$$$
  269.             if( MMIsObject(object) && somIsObj(object) )
  270.                 delete object;
  271.             else
  272.                 ODDisposePtr(object);
  273.             ODDisposePtr( key );
  274.         }
  275.     
  276.         ODDeleteObject( iter );
  277.     }CATCH_ALL{
  278.         // ignore exception
  279.     }ENDTRY
  280. }
  281.  
  282. //------------------------------------------------------------------------------
  283. // ODObjectNameSpace: Purge
  284. //------------------------------------------------------------------------------
  285.  
  286. SOM_Scope ODSize  SOMLINK ODObjectNameSpacePurge(ODObjectNameSpace *somSelf, Environment *ev,
  287.         ODSize size)
  288. {
  289.     /* ODObjectNameSpaceData *somThis = ODObjectNameSpaceGetData(somSelf); */
  290.     ODObjectNameSpaceMethodDebug("ODObjectNameSpace","Purge");
  291.  
  292.     ODUnused(size);
  293.     return 0;
  294. }
  295.  
  296. //------------------------------------------------------------------------------
  297. // ODObjectNameSpace: Register
  298. //------------------------------------------------------------------------------
  299.  
  300. SOM_Scope void  SOMLINK ODObjectNameSpaceRegister(ODObjectNameSpace *somSelf, Environment *ev,
  301.         ODISOStr key, ODObject* object)
  302. {
  303.     /* ODObjectNameSpaceData *somThis = ODObjectNameSpaceGetData(somSelf); */
  304.     ODObjectNameSpaceMethodDebug("ODObjectNameSpace","Register");
  305.     
  306.     if (somSelf->GetType(ev) != kODNSDataTypeODObject)
  307.         THROW(kODErrInvalidNSType);
  308.  
  309.     SOM_TRY
  310.  
  311.     ODULong len = ODISOStrLength(key);
  312.     ODULong valueLength = sizeof(ODObject);
  313.     somSelf->GetHashTable(ev)->Insert((ODUByte*)key, len, (ODPtr)object, valueLength);
  314.     
  315.     SOM_CATCH_ALL
  316.     SOM_ENDTRY
  317. }
  318.  
  319.  
  320. //------------------------------------------------------------------------------
  321. // ODObjectNameSpace: GetEntry
  322. //------------------------------------------------------------------------------
  323.  
  324. SOM_Scope ODBoolean  SOMLINK ODObjectNameSpaceGetEntry(ODObjectNameSpace *somSelf, Environment *ev,
  325.         ODISOStr key, ODObject** object )
  326. {
  327.     /* ODObjectNameSpaceData *somThis = ODObjectNameSpaceGetData(somSelf); */
  328.     ODObjectNameSpaceMethodDebug("ODObjectNameSpace","GetEntry");
  329.  
  330.     ODBoolean    result = kODFalse;
  331.     *object = kODNULL;
  332.     
  333.     SOM_TRY
  334.  
  335.     if (somSelf->GetType(ev) == kODNSDataTypeODObject)
  336.     {
  337.         StringHashTable*    strHashTable = somSelf->GetHashTable(ev);
  338.         if (strHashTable)
  339.         {
  340.             ODULong        objLen;
  341.             size_t         len = strlen(key);
  342.             result = strHashTable->Find((ODUByte*)key, len, (ODPtr*)object, &objLen);
  343.         }
  344.         
  345.         if ( result == kODFalse )
  346.         {
  347.             ODObjectNameSpace *parent = (ODObjectNameSpace*)somSelf->GetParent(ev);
  348.             if (parent)
  349.             {
  350.                 if (parent->GetType(ev) == kODNSDataTypeODObject)
  351.                     result = parent->GetEntry(ev, key, object);
  352.             }
  353.         }
  354.     }
  355.     
  356.     SOM_CATCH_ALL
  357.         result = kODFalse;
  358.     SOM_ENDTRY
  359.  
  360.     if ( !result )
  361.         *object = kODNULL;
  362.  
  363.     return result;
  364. }
  365.  
  366. //------------------------------------------------------------------------------
  367. // ODObjectNameSpace: CreateIterator
  368. //------------------------------------------------------------------------------
  369.  
  370. SOM_Scope ODObjectIterator*  SOMLINK ODObjectNameSpaceCreateIterator(ODObjectNameSpace *somSelf, Environment *ev)
  371. {
  372.     /* ODObjectNameSpaceData *somThis = ODObjectNameSpaceGetData(somSelf); */
  373.     ODObjectNameSpaceMethodDebug("ODObjectNameSpace","CreateIterator");
  374.     
  375.     ODObjectIterator* theObjectIterator = kODNULL;
  376.  
  377.     SOM_TRY
  378.     
  379.     theObjectIterator = new ODObjectIterator;
  380.     THROW_IF_NULL(theObjectIterator);
  381.     theObjectIterator->InitObjectIterator(ev, somSelf);
  382.     
  383.     SOM_CATCH_ALL
  384.     SOM_ENDTRY
  385.  
  386.     return theObjectIterator;
  387. }
  388.